package com.cmcc.pcap.packet.sctp.ngaps;

import com.cmcc.packet.entity.NGAP.NgapEntity;
import com.cmcc.pcap.packet.sctp.SctpConnection;
import com.cmcc.pcap.packet.sctp.SctpProtocolHandler;
import com.cmcc.pcap.packet.sctp.ngaps.xdr.NgapXdrGenerate;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Date;
import java.util.List;

public class NgapHandler extends SctpProtocolHandler<NgapEntity> {

    private static Logger logger = LogManager.getLogger(NgapHandler.class);
    private NgapXdrGenerate nxg;

    public NgapHandler() {
        nxg =  new NgapXdrGenerate();
    }

    @Override
    public List<NgapEntity> handle(SctpConnection sctpConnection, String taskId) {

        logger.debug("start execute ngap packet handler");
        logger.debug(new Date(sctpConnection.lastPacketTime));
        long pcapTimestamp = (sctpConnection.al.get(sctpConnection.al.size() - 1)).timestamp.getTime()*1000L;
        logger.debug(new Date(pcapTimestamp));

        /**
         * SCTP Connection，去掉重复DATA报文和重复SACK报文
         */
        sctpConnection.sctpConnectionPacketsDeal();
        logger.debug("this connection <"+sctpConnection.connectionKey+"> hava packets (initial)  : "+sctpConnection.packetsNumInitial);
        logger.debug("this connection <"+sctpConnection.connectionKey+"> hava packets (after retransmission merge)  : "+sctpConnection.packetsNumAfterRetransmissionDataMerge);

        com.cmcc.pcap.packet.sctp.ngaps.NgapMessages ngapmessages = com.cmcc.pcap.packet.sctp.ngaps.NgapMessagesFilter.filter(sctpConnection);
        List<NgapEntity> xdrNgapEntityList = this.nxg.generate(ngapmessages,taskId);
        logger.debug("this connection <" + sctpConnection.connectionKey + "> saved into database !");
        return xdrNgapEntityList;

    }
}
